package com.zyb.warehouse.service.impl;

import com.zyb.warehouse.common.Result;
import com.zyb.warehouse.entity.ProductType;
import com.zyb.warehouse.mapper.ProductTypeMapper;
import com.zyb.warehouse.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:分类表业务层接口实现类
 */
@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    /**
     * 查询商品分类
     *
     * @return
     */
    public List<ProductType> queryCategoryTree() {
        //查询所有商品分类
        List<ProductType> typeList = productTypeMapper.selectAllCategory();
        //将所有商品分类List<ProductType>转成商品分类树List<ProductType>
        List<ProductType> productTypeList = recursionSelect(typeList, 0);
        //返回商品分类树List<ProductType>
        return productTypeList;
    }

    //递归算法查询二级分类
    private List<ProductType> recursionSelect(List<ProductType> allList, Integer pid) {
        //用来存放商品分类
        List<ProductType> TypeList = new ArrayList<>();
        for (ProductType productType : allList) {
            if (productType.getParentId().equals(pid)) {
                TypeList.add(productType);
            }
        }

        for (ProductType productType : TypeList) {
            List<ProductType> childTypeList = recursionSelect(allList, productType.getTypeId());
            productType.setChildProductCategory(childTypeList);
        }
        return TypeList;
    }

    /**
     * 验证商品分类编号是否重复
     *
     * @param typeCode
     * @return
     */
    public Result verifyTypeCode(String typeCode) {
        ProductType productType = productTypeMapper.selectProductCategoryByTypeCode(typeCode);
        return Result.ok(productType == null);
    }

    /**
     * 添加商品分类
     *
     * @param productType
     * @return
     */
    @Transactional
    public Result addProductCategory(ProductType productType) {
        //查询商品分类是否已存在
        ProductType pt = productTypeMapper.selectProductCategoryByTypeName(productType.getTypeName());
        if (pt != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "该分类已存在");
        }
        //添加商品分类
        Integer i = productTypeMapper.insertProductCategory(productType);
        if (i > 0) {
            return Result.ok("商品分类添加成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "商品分类添加失败");
    }

    /**
     * 删除商品分类
     *
     * @param typeId
     * @return
     */
    @Transactional
    public Result removeProductCategory(Integer typeId) {
        Integer i = productTypeMapper.deleteProductCategory(typeId);
        if (i > 0) {
            return Result.ok("删除成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "删除失败");
    }

    /**
     * 修改商品分类
     *
     * @param productType
     * @return
     */
    @Transactional
    public Result modifyProductCategory(ProductType productType) {
        //查询商品分类是否已存在
        ProductType pt = productTypeMapper.selectProductCategoryByTypeName(productType.getTypeName());
        if (pt != null && !pt.getTypeId().equals(productType.getTypeId())) {
            return Result.err(Result.CODE_ERR_BUSINESS, "该分类已存在");
        }
        //进行修改
        Integer i = productTypeMapper.updateProductCategory(productType);
        if (i > 0) {
            return Result.ok("修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "修改失败");
    }
}
